#!/bin/bash
# $1 text file (..hopefully prepared for RTTY..)
# minimodem -i -q --rx rtty -a --alsa=CODEC
# rigctld -T localhost -m 1 -r /dev/ttyUSB0 -p /dev/ttyUSB1 -P RTS

CALLSIGNBANNER=/home/cjb/rtty/callsign-one-minute.wav
TXAUDIOSCRIPT=/home/cjb/Bin/tx_wav.sh
RTTYWAV=/tmp/$$.r.wav
TONEWAV=/tmp/$$.t.wav
TXRTTYWAV=/tmp/$$.tx.wav
TMPWAV=/tmp/$$.tmp.wav

echo "Input details: (~3400/chars in 10 minutes)"
wc $1|awk '{print "Characters "$3", Lines "$1}'

cat $1| sed -e 's/$/\r/g' | \
minimodem -5 --stopbits 1.5 -M 2125 -S 2295 --write rtty -f $RTTYWAV

echo -n "Generated audio file length: "
dur=`soxi -D $RTTYWAV`
echo $dur

sox --null $TONEWAV synth $dur sine 91.5
sox --combine mix --volume 0.74 $RTTYWAV --volume 0.24 $TONEWAV $TMPWAV
mv -f $TMPWAV $RTTYWAV

echo "Mixing with "$CALLSIGNBANNER" (ignore EOF warning) "
sox $CALLSIGNBANNER -p repeat 60 trim 0 $dur \
    | sox --volume 0.66 - --combine mix --volume 0.65 $RTTYWAV -t wav $TXRTTYWAV lowpass 2500 gain 1.25

ls -l $TXRTTYWAV

###

RIG=2
RADDEV=localhost:4532

a=`pgrep rigctld`X
userig="0"
if [ "$a" == "X" ]
then
    echo "Starting rigctld"
    rigctld -T localhost -m 1 -s 38400 -r /dev/ttyUSB0 -p /dev/ttyUSB1 -P RTS &
    sleep 1
    userig="1"
fi

###

echo -n "^C to abort, Enter to start transmission >"
read a

rigctl -m $RIG -r $RADDEV T 1
sleep 1
echo -n "Transmitting audio"
mplayer -msglevel all=-1:statusline=5 -ao alsa:device=hw=CODEC $TXRTTYWAV
sleep 1
rigctl -m $RIG -r $RADDEV T 0

###

if [ userig == "1" ]
then
    echo "Killing rigctld"
    killall rigctld
fi

rm -f $RTTYWAV $TONEWAV $TXRTTYWAV $TMPWAV



